package cm.mefobe.android.telecomcost.view;

import cm.mefobe.android.telecomcost.dialog.TimePeriodPickerDialog;
import cm.mefobe.android.telecomcost.entities.TimePeriod;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TimePeriodPickerView extends TableLayout {

	private Context _context;
	private TextView _label;
	//private EditText _txtPeriod;
	private Button _btnSetPeriod;
	private TimePeriodPickerDialog dialog;
	private TimePeriod _period;
	private TableRow row;

	public TimePeriod getPeriod() {
		return _period;
	}

	public void setPeriod(TimePeriod period) {
		this._period = period;
		String _period = period.toString();		
		
		_btnSetPeriod.setText(_period);		
	}

	private void setTimePeriodPickerView() {
		if (isInEditMode()) {
			Log.i("TimePeriodPickerView", "isInEditMode");
		} else {
			if (row == null) {
				Log.i("TimePeriodPickerView", "adding views...");
				row = new TableRow(_context);				
				//_txtPeriod = new EditText(_context);
				_btnSetPeriod = new Button(_context);
				
				_btnSetPeriod.setText(TimePeriod.SECOND.toString());
				//_txtPeriod.setEnabled(false);

				//_btnSetPeriod.setText("Pick");
				_btnSetPeriod.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						showTimePeriodPickerDialog();
					}
				});
				
				//row.addView(_txtPeriod);
				row.addView(_btnSetPeriod);

				addView(row);	
				setColumnStretchable(0,true);
			} else {
				Log.i("TimePeriodPickerView", "TimePeriodPickerView already set");
			}
		}
	}

	public TimePeriodPickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("TimePeriodPickerView",
				"creating the time picker view with AttributeSet");

		_context = context;		
	}

	public TimePeriodPickerView(Context context) {
		super(context);
		Log.i("TimePeriodPickerView", "creating the TimePeriodPickerView");
		_context = context;
		setTimePeriodPickerView();
	}

	protected void showTimePeriodPickerDialog() {
		dialog = new TimePeriodPickerDialog(_context,this);
		dialog.show();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("TimePeriodPickerView", "onLayout");
		super.onLayout(changed, l, t, r, b);
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("TimePeriodPickerView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("TimePeriodPickerView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("TimePeriodPickerView", "onFinishInflate");
		super.onFinishInflate();		
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("TimePeriodPickerView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public void setLabel(String label) {
		if (_label != null) {
			_label.setText(label);
		} else {
			Log.i("TimePeriodPickerView", "cannot set the label...textView is null");
		}
	}
}
